<?php
namespace Admin\Controller;
use Common\Model\UserCustomerModel;
use Common\Model\UserModel;
use Common\Model\UserInfoModel;
use Think\Page;
use Common\Model\LogModel;

/**
 * 客户控制器
 * 
 * @author xiaoyutab<xiaoyutab@qq.com>
 * @version v1.0.0
 * @copyright (c) 2017, xiaoyutab
 * @adtime 2018-06-01 17:51:30
 */
class CustomerController extends BashController{
    /**
     * 客户控制器
     * 
     * @return void
     * @author xiaoyutab<xiaoyutab@qq.com>
     * @copyright (c) 2017, xiaoyutab
     * @version v1.0.0
     * @adtime 2018-06-01 17:51:30
     */
    public function listAction(){
        $user = new UserCustomerModel();
        $where = array();
        foreach(explode(' ', I('get.keywords')) as $v){
            $where[] = '%'.$v.'%';
        }
        $wheres = array(
            'name|phone|company' => array('like',$where),
        );
        // 如果登录的用户是业务员组的且不是组长【专员自己】
        if(session('u_info.admin_group') == 2 && session('u_info.admin_group_leader') != 1){
            $wheres['uid'] = session('u_info.id');
        } else {
            $arrs = [];
            $_user = new UserModel();
            $arr = $_user->where(['username'=>['like',$where]])->select();
            foreach($arr as $v){
                $arrs[] = $v['id'];
            }
            $_user = new UserInfoModel();
            $arr = $_user->where(['nickname'=>['like',$where]])->select();
            foreach($arr as $v){
                $arrs[] = $v['id'];
            }
            $wheres['uid'] = ['in',$arrs];
            $wheres['_logic'] = 'or';
        }
        $count = $user->where($wheres)->getCount();
        $page = new Page($count, 10);
        $list = $user->where($wheres)->getList($page->firstRow, $page->listRows);
        return $this->assign(array(
            'count' => $count,
            'list' => $list,
            'page' => $page->show(),
        ))->display();
    }
    
    /**
     * 添加客户信息
     * @return type
     */
    public function createAction(){
        $this->wget('bootstrap-select2');
        if(!I('post.')){
            return $this->display();
        }
        $model = new UserCustomerModel();
        $id = $model->create_info(I('post.'));
//        if($id){
        $log = new LogModel();
        $log->create_oth_log($id, 'user_customer');
        return $this->success('添加成功',U('Customer/list'));
//        }
//        return $this->error('系统错误');
    }
    
    /**
     * 修改客户信息
     * @return type
     */
    public function editAction(){
        $id = I('get.id',0);
        $model = new UserCustomerModel();
        $info = $model->get_info($id);
        if(!I('post.')){
            $this->wget('bootstrap-select2');
            $this->assign('info',$info);
            $this->assign('uid_info',(new UserModel())->get_info($info['uid']));
            return $this->display('create');
        }
        $model->edit_info($id,I('post.'));
//        if($model->edit_info($id,I('post.'))){
        $log = new LogModel();
        $log->update_admin_date('user_customer', I('post.'), $info);
        return $this->success('修改成功',U('list'));
//        }
//        return $this->error('系统错误');
    }
    
    /**
     * 删除客户信息
     * @return type
     */
    public function delAction(){
        $model = new UserCustomerModel();
        $model->setDelete(I('get.id'));
//        if($model->setDelete(I('get.id'))){
        (new LogModel())->delete_admin_log(I('get.id'),'user_customer');
        return $this->success('删除成功',U('list'));
//        }
//        return $this->error('系统错误');
    }
}